﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using V5_EShop.Data;
using System.Data;

namespace V5_EShop.Admin {
    public partial class Admin_HotKeyWord : AdminPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["action"]);
                this.Bind_DropDownList();
                switch (Action.ToLower().Trim()) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_DataList();
                        break;
                }
            }
        }

        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;

            DataTable dt = V5.SQL.Query_Pager("*", "(Select B.ClassTitle,A.* From Tbl_HotKeyWord A Left Join Tbl_HotKeyWordClass B On A.ClassId=B.ClassId) AA", "", "Id Desc", Pager.PageSize, Pager.CurrentPageIndex, ref RecordCount);

            this.Pager.RecordCount = RecordCount;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        private void Bind_DropDownList() {
            DataTable dt = V5.Class.GetDropDownList("Tbl_HotKeyWordClass", 0, "请选择");
            this.ddlClassId.DataSource = dt;
            this.ddlClassId.DataTextField = "ClassTitle";
            this.ddlClassId.DataValueField = "ClassId";
            this.ddlClassId.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            ModelHotKeyWord model = new ModelHotKeyWord();
            int id = InputHelper.SetNumber(this.hfID.Value);
            int ClassId = InputHelper.SetNumber(this.ddlClassId.SelectedValue);
            string HotKeyName = this.txtKeyWordTitle.Text;
            string HotKeyUrl = this.txtKeyWordUrl.Text;
            int IsShow = InputHelper.SetNumber(this.ddlIsShow.SelectedValue);
            model.ClassId = ClassId;
            model.HotKeyName = HotKeyName;
            model.HotKeyUrl = HotKeyUrl;
            model.IsShow = IsShow;
            model.OrderID = 0;
            model.CreateTime = DateTime.Now;
            if (id == 0) {
                model.Add();
            }
            else if (id > 0) {
                model.ID = id;
                model.Update();
            }
            this.Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int id = InputHelper.SetNumber(e.CommandArgument);
            switch (eName.ToLower().Trim()) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelHotKeyWord model = new ModelHotKeyWord(p => p.ID == id);
            if (model != null) {
                this.ddlClassId.SelectedIndex =
                  this.ddlClassId.Items.IndexOf(
                      this.ddlClassId.Items.FindByValue(model.ClassId.Value.ToString())
                  );
                this.txtKeyWordTitle.Text = model.HotKeyName;
                this.txtKeyWordUrl.Text = model.HotKeyUrl;
                this.ddlIsShow.SelectedIndex =
                    this.ddlIsShow.Items.IndexOf(
                        this.ddlIsShow.Items.FindByValue(model.IsShow.ToString())
                    );
                this.hfID.Value = model.ID.ToString();
                this.btnSave.Text = "修改保存";
                this.lblTitle.Text = "修改热门关键字";
            }
        }

        private void Bind_Delete(int id) {
            new ModelHotKeyWord(p => p.ID == id).Delete();
            this.Bind_DataList();
        }
    }
}
